# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import csv
import logging
import os
logger = logging.getLogger(__name__)

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class DoubanPipeline:
    def process_item(self, item, spider):
        type = item.get('type')
        logger.info('type is %s' % type)
        if spider.name == 'top250' and type == 'info':
            download_path = os.getcwd() + os.sep + 'download'
            if not os.path.exists(download_path):
                os.mkdir(download_path)

            with open(download_path + os.path.sep + '豆瓣电影TOP250.csv','a',encoding='utf-8',newline='') as f:
                f_csv = csv.DictWriter(f, ['id', 'img_src', 'title', 'director', 'info', 'score', 'comment_num', 'quote'])
                item.pop('type')
                f_csv.writerow(item)
                print("保存信息到CSV....ok")

        elif spider.name == 'top250' and type == 'img':
            download_path = os.getcwd() + os.sep + 'download'
            if not os.path.exists(download_path):
                os.mkdir(download_path)
            filename = item.get('filename')
            img = item.get('img')
            with open(download_path + os.path.sep + filename,'wb') as f:
                f.write(img)
                print("保存图片到本地....ok")

        return item